/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSWindowTemplate.h"
#include "NSKeyedUnarchiver.h"
#include "NSString.h"

IMPLEMENT_DYNAMIC_CLASS(NSWindowTemplate, NSObject)

void NSWindowTemplate::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	NSObject::initWithUnarchiver(archiver);
	if (archiver->contains(wxT("NSViewClass")))
		archiver->decodeObjectForKey(wxT("NSViewClass"));
	if (archiver->contains(wxT("NSWindowClass")))
	{
		wxObject *wc_o = archiver->decodeObjectForKey(wxT("NSWindowClass"));
		wxVariant *v = wxDynamicCast(wc_o, wxVariant);
		if (v)
			_windowClass = new wxString(v->GetString());
		else {
			NSMutableString *s = wxDynamicCast(wc_o, NSMutableString); /* really should get rid of wxVariant at all! */
			wxASSERT(s);
			_windowClass = new wxString(*s);
		}
	}
	if (archiver->contains(wxT("NSWindowStyleMask")))
		_windowStyle = archiver->decodeIntForKey(wxT("NSWindowStyleMask"));
	if (archiver->contains(wxT("NSWindowBacking")))
		archiver->decodeIntForKey(wxT("NSWindowBacking"));
	if (archiver->contains(wxT("NSWindowView")))
	{
		_view = wxDynamicCast(archiver->decodeObjectForKey(wxT("NSWindowView")), NSView);
		wxASSERT(_view != NULL);
	}
	if (archiver->contains(wxT("NSWTFlags")))
		_flags = archiver->decodeIntForKey(wxT("NSWTFlags"));
	if (archiver->contains(wxT("NSMinSize")))
		archiver->decodeSizeForKey(wxT("NSMinSize"));
	if (archiver->contains(wxT("NSMaxSize")))
		archiver->decodeSizeForKey(wxT("NSMaxSize"));
	if (archiver->contains(wxT("NSWindowRect")))
	{
		_windowRect = archiver->decodeRectForKey(wxT("NSWindowRect"));
		wxASSERT(_windowRect != NULL);
	}
	if (archiver->contains(wxT("NSFrameAutosaveName")))
		archiver->decodeStringForKey(wxT("NSFrameAutosaveName"));
	if (archiver->contains(wxT("NSWindowTitle")))
		_title = archiver->decodeStringForKey(wxT("NSWindowTitle"));
}

NSObject* NSWindowTemplate::nibInstantiate(NSKeyedUnarchiver* archiver)
{
	if (!_realObject)
	{
		_realObject = wxDynamicCast(NSObject::createDynamicObject(*_windowClass), NSWindow);
		wxASSERT(_realObject);
		_realObject->initWithRect(_windowRect, _windowStyle, !!(_flags & WTF_DEFERRED));
		_realObject->setContentView(_view);
		_realObject->setTitle(_title);
		_view->_fixSubviews();
		NSMutableArray *sviews = _realObject->contentView()->subviews();
		if (sviews)
		  for(size_t i=0;i<sviews->length();i++)
		  {
			  wxDynamicCast(sviews->getObject(i), NSObject)->nibInstantiate(archiver);
		  }
	}
	return _realObject;
}
